You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or twosteps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
給一個 array cost 紀錄每一步的花費,每次可以前進一步或兩部,只能從 cost 的 index = 0 或 index = 1 開始,找出花費最少的值。
 
 
1 已經算過並放在 dp[0] 中將他和 30 相加 
2 已經算過並放在 dp[1] 中將他和 30 相加 
30 所花費的最少金額 
2 已經算過並放在 dp[1] 中將他和 5 相加 
3 已經算過並放在 dp[2] 中將他和 5 相加 
5 所花費的最少金額 
3 已經算過並放在 dp[2] 中將他和 20 相加 
4 已經算過並放在 dp[3] 中將他和 20 相加 
20 所花費的最少金額 
 
 
dp[3], dp[4] 分別紀錄了走到 5 和走到 20 所花費的最少金額,所以可以看出從 5 走兩步離開樓梯金額是最少的。var minCostClimbingStairs = function(cost) {
    const dp = [cost[0], cost[1]];
    for(let i = 2; i < cost.length; i++) {
        dp[i] = cost[i] + Math.min(dp[i - 1], dp[i - 2]);
    }
    return Math.min(dp[dp.length - 1], dp[dp.length - 2]);
};